import Taro, {Component} from "@tarojs/taro";
import {View, Image, Text} from "@tarojs/components";
import {toJS} from 'mobx'
import {inject, observer} from '@tarojs/mobx'
import {IStoreProps} from '../../store/Stores'
import {getRuntime, pageSize, partOf} from '../../utils/common'

import './index.less'

interface IHomeProps extends IStoreProps {}

@inject("stores")
@observer
class WelfareRecord extends Component<IHomeProps> {
  constructor(props) {
    super(props)
  }

  state = {
    pageIndex:1,
    pageSize,
    items: [],
    totalCount:0,
  }

  componentWillMount(): void {
    Taro.setNavigationBarTitle({ title:'资助记录' })
    this.getAllUserCrowdFunding()
  }

  getAllUserCrowdFunding() {
    let { pageIndex,pageSize } = this.state
    let { userStore } = this.props.stores
    userStore.getAllUserCrowdFunding({
      pageIndex,
      pageSize,
      callback: res => {
        if (pageIndex === 1) {
          this.setState({totalCount: toJS(res.totalCount), items: toJS(res.items)})
        }else {
          const { items } = this.state;
          items.push(...res.items || []);
          this.setState({totalCount: res.totalCount, items})
        }
      }
    })
  }

  onReachBottom(): void {
    let {totalCount, pageSize, pageIndex} = this.state
    const {loading} = this.props.stores.homeStore;
    if (totalCount > pageIndex * pageSize && !loading) {
      this.setState({
        pageIndex: pageIndex + 1
      }, () => {
        this.getAllUserCrowdFunding()
      })
    }
  }

  handelRouter(url: string = '/pages/index/index'): void {
    getRuntime().navigateTo(url)
  }

  render() {
    let { items } = this.state
    return (
      <View className='welfare-index'>
        {items.length>0&&
        <View className='list-16'>
          {
            items.map(item => {
              return(
                <View className='list-16-item' onClick={this.handelRouter.bind(this,'/pages/public_welfare/welfare_record_detail?id='+item.id)}>
                  <Image className='img' src={item.logoImg} />
                  <View className='info'>
                    <View className='list-title'>{item.name}</View>
                    <View className='list-desc'>{partOf(item.introduce,50)}</View>
                    <View className='price'>捐助金额：
                      <Text className='list-small color-orange'>￥</Text>
                      <Text className='list-price color-orange'>{item.actualAmount}</Text>
                    </View>
                  </View>
                </View>
              )
            })
          }
        </View>
        }
        {items.length<=0&&<View className='page-none-info'>您还没有公益记录哦~</View>}
      </View>
    )
  }
}

export default WelfareRecord
